package TestModel;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import DataModel.LayoutManager.CEdge;
import DataModel.SegmentTree.SegmentTree;
import DataModel.SegmentTree.SegmentTreeImpl;
import DataModel.SegmentTree.SegmentTreeIterator;
import DataModel.SegmentTree.SegmentTreeNode;
import DataModel.SegmentTree.SegmentTreeNodeImpl;
import DataModel.SegmentTree.SegmentTreePostorderIterator;
import DataModel.SegmentTree.SegmentTreePreorderIterator;
import junit.framework.TestCase;

public class SegmentTreeTest extends TestCase {
	CEdge c1 ;
	CEdge c2 ;
	CEdge c3 ;
	CEdge c4 ;
	CEdge c5 ;
	CEdge c6 ;
	CEdge c7 ;
	CEdge c8 ;
	
	SegmentTree tree;
	
	SegmentTreeNode t1;
	SegmentTreeNode t2;
	SegmentTreeNode t3;
	SegmentTreeNode t4;
	SegmentTreeNode t5;
	SegmentTreeNode t6;
	SegmentTreeNode t7;
	SegmentTreeNode t8;
	SegmentTreeNode t9;
	SegmentTreeNode t10;
	SegmentTreeNode t11;
	SegmentTreeNode t12;
	SegmentTreeNode t13;
	SegmentTreeNode t14;
	SegmentTreeNode t15;
	SegmentTreeNode t16;
	SegmentTreeNode t17;
	SegmentTreeNode t18;
	SegmentTreeNode t19;
	SegmentTreeNode t20;
	SegmentTreeNode t21;
	SegmentTreeNode t22;
	SegmentTreeNode t23;
	SegmentTreeNode t24;
	SegmentTreeNode t25;
	SegmentTreeNode t26;
	SegmentTreeNode t27;
	
	
	Queue<SegmentTreeNode> testNodes;
	
	ArrayList<CEdge> edges;
	
	public void setUp() {
		testNodes  = new LinkedList<SegmentTreeNode>();
		
		t1 = new SegmentTreeNodeImpl(3, 587);
		t2 = new SegmentTreeNodeImpl(3, 39);
		t3 = new SegmentTreeNodeImpl(39, 587);
		t4 = new SegmentTreeNodeImpl(3, 19);
		t5 = new SegmentTreeNodeImpl(19, 39);
		t5.insertData(4);
		t5.insertData(6);
		t5.insertData(8);
		
		t6 = new SegmentTreeNodeImpl(39, 51);
		t6.insertData(4);
		t6.insertData(6);
		t6.insertData(8);
		
		
		t7 = new SegmentTreeNodeImpl(51, 587);
		t8 = new SegmentTreeNodeImpl(3, 4);
		t8.insertData(3);
		t9 = new SegmentTreeNodeImpl(4, 19);
		t10 = new SegmentTreeNodeImpl(19, 30);
		t11 = new SegmentTreeNodeImpl(30, 39);
		t11.insertData(1);
		t11.insertData(2);
		
		t12 = new SegmentTreeNodeImpl(39, 40);
		t12.insertData(1);
		t12.insertData(2);
		
		t13 = new SegmentTreeNodeImpl(40, 51);
		t14 = new SegmentTreeNodeImpl(51, 361);
		t14.insertData(8);
		
		t15 = new SegmentTreeNodeImpl(361,587);
		t15.insertData(5);
	
		t16 = new SegmentTreeNodeImpl(4,5);
		t17 = new SegmentTreeNodeImpl(5,19);
		t17.insertData(6);
		t17.insertData(8);
		t18 = new SegmentTreeNodeImpl(19,20);
		t19 = new SegmentTreeNodeImpl(20,30);
		t19.insertData(1);
		t20 = new SegmentTreeNodeImpl(30,37);
		t21 = new SegmentTreeNodeImpl(37,39);
		t21.insertData(7);
		
		t22 = new SegmentTreeNodeImpl(40,48);
		t22.insertData(1);
	
		t23 = new SegmentTreeNodeImpl(48,51);
		t24 = new SegmentTreeNodeImpl(51,52);
		t24.insertData(6);
		t25 = new SegmentTreeNodeImpl(52,361);
		t26 = new SegmentTreeNodeImpl(361,460);
		t26.insertData(8);
		t27 = new SegmentTreeNodeImpl(460,587);
		
		testNodes.add(t1);
		testNodes.add(t2);
		testNodes.add(t3);
		testNodes.add(t4);
		testNodes.add(t5);
		testNodes.add(t6);
		testNodes.add(t7);
		testNodes.add(t8);
		testNodes.add(t9);
		testNodes.add(t10);
		testNodes.add(t11);
		testNodes.add(t12);
		testNodes.add(t13);
		testNodes.add(t14);
		testNodes.add(t15);
		testNodes.add(t16);
		testNodes.add(t17);
		testNodes.add(t18);
		testNodes.add(t19);
		testNodes.add(t20);
		testNodes.add(t21);
		testNodes.add(t22);
		testNodes.add(t23);
		testNodes.add(t24);
		testNodes.add(t25);
		testNodes.add(t26);
		testNodes.add(t27);
		
		
		c1 =  new CEdge(10,20,10,48,1,null);
		c2 =  new CEdge(10,30,10,40,2,null);
		c3 =  new CEdge(10,3,10,4,3,null);
		c4 =  new CEdge(10,19,10,51,4,null);
		c5 =  new CEdge(10,361,10,587,5, null);
		c6 =  new CEdge(10,5,10,52,6,null);
		c7 =  new CEdge(10,37,10,39,7,null);
		c8 =  new CEdge(10,5,10,460,8,null);
		
		

		edges = new ArrayList<CEdge>();
	
		edges.add(c1);
		edges.add(c2);
		edges.add(c3);
		edges.add(c4);
		edges.add(c5);
		edges.add(c6);
		edges.add(c7);
		edges.add(c8);
		
		tree = new SegmentTreeImpl(edges, 'y');
	}
	public void testSegmentTree() {
		
		tree.insert(c1);
		tree.insert(c2);
		tree.insert(c3);
		tree.insert(c4);
		tree.insert(c5);
		tree.insert(c6);
		tree.insert(c7);
		tree.insert(c8);
	
		SegmentTreeImpl.printSegmentTree(tree);
		SegmentTreeNode root = tree.getRoot();

		Queue<SegmentTreeNode> q = new LinkedList<SegmentTreeNode>();
		q.add(root);
		while (!testNodes.isEmpty()) {
			SegmentTreeNode t = q.poll();
			long b= t.getBegining();
			long e= t.getEnd();
			ArrayList<Long> data = t.getData();
			SegmentTreeNode testNode = testNodes.poll();
			long btest = testNode.getBegining();
			long etest = testNode.getEnd();
	    	ArrayList<Long> dtest =testNode.getData();
			assertEquals("Begin :",btest,b);
			assertEquals("End :",etest,e);
			assertEquals("Number of intervasl: ",dtest.size(),data.size());
			for (int i =0; i<dtest.size();i++) {
				assertEquals("Id of edge: ",dtest.get(i),data.get(i));
				
			}
			if (t.leftSon() != null)  {
				q.add(t.leftSon());
			}
			if (t.rightSon() != null)  {
				q.add(t.rightSon());
			}			
			
		}

	}
	
	

}
